View Javadoc

1   /*
2    * Copyright 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * AbstractQueryParam.java
6    *
7    * Created on 7-Dec-2004 at 12:53:55 PM
8    */
9   package ca.uhn.cache.impl;
10  
11  import java.io.Serializable;
12  import java.util.Arrays;
13  
14  import ca.uhn.cache.IDimension;
15  import ca.uhn.cache.IGroupQueryParam;
16  import ca.uhn.cache.IPointQueryParam;
17  import ca.uhn.cache.IQueryParam;
18  
19  
20  /***
21   * Base class for all <code>IQueryParam</code> implementations. 
22   * 
23   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
24   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:52:34 $ by $Author: bryan_tripp $
25   */
26  public abstract class AbstractQueryParam extends CommonsLangObject implements IQueryParam, Comparable, Serializable {
27  
28      private final IDimension myDimension;
29  
30      /***
31       * @param theDimension The dimension where this parameter is difined.
32       */
33      protected AbstractQueryParam( IDimension theDimension ) {
34          assert theDimension != null;
35          assert Arrays.asList( theDimension.getParamTypes() ).contains( this.getClass() );
36          
37          myDimension = theDimension; 
38      }
39  
40      /***
41       * @see ca.uhn.cache.IQueryParam#getDimension()
42       */
43      public IDimension getDimension() {
44          return myDimension;
45      }
46      
47      /***
48       * {@inheritDoc}
49       */
50      public boolean intersects( IQueryParam theParam ) {
51          return compatibleWith(theParam);
52      }
53  
54      /***
55       * By default only checks compatibility with other AbstractQueryParam 
56       * descendents (can be overridden).
57       * 
58       * @param theParam another param
59       * @return true iff the other param has the same dimension and the same 
60       *      point param class.   
61       */
62      protected boolean compatibleWith(IQueryParam theParam) {
63          boolean retVal = false;
64          
65          if (getPointClass(this).equals(getPointClass(theParam)) 
66                  && this.getDimension().equals(theParam.getDimension())) {
67              retVal = true;
68          }
69          
70          return retVal;        
71      }
72      
73      //we can clean this up by making all IQueryParams either point or group params 
74      //and moving getPointParamClass() to IQueryParam, but for now the mess is confined here 
75      private Class getPointClass(IQueryParam theParam) {
76          if (theParam instanceof IPointQueryParam) {
77              return theParam.getClass();
78          } else if (theParam instanceof IGroupQueryParam) {
79              return ((IGroupQueryParam) theParam).getPointParamClass();
80          } else {
81              throw new IllegalArgumentException("Only IPointQueryParam and IGroupQueryParam are " +
82                      "supported, not " + theParam.getClass().getName());
83          }
84      }
85      
86      /***
87       * @return the class of the point parameter associated with this type, 
88       *      for example DateParam for DateRangeParam (defaults to this.getClass())  
89       */
90      protected Class getPointParamClass() {
91          return this.getClass();
92      }
93  
94      /***
95       * {@inheritDoc}
96       */
97      public int compareTo( Object theObj ) {
98          
99          if ( !(theObj instanceof IQueryParam) ) {
100             throw new ClassCastException( "theObj must be a " + IQueryParam.class.getName() );
101         }
102         IQueryParam qp = (IQueryParam) theObj;
103         String dimensionName = getDimension().getName();
104         String otherDimensionName = qp.getDimension().getName();
105         
106         //make sure the query params are always added to sorted containers in the same order
107         return dimensionName.compareTo( otherDimensionName );
108     }
109 }